package mesweb.mesweb.controller.device;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.dto.device.ProcedureDeviceCapacityDto;
import mesweb.mesweb.entity.device.MesDevice;
import mesweb.mesweb.service.impl.device.MesDeviceServiceImpl;
import mesweb.mesweb.vo.MesDeviceVo;
import mesweb.mesweb.vo.device.ProcedureDeviceParameterVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author niufw
 * @since 2021-05-19
 */
@RestController
@RequestMapping("/mes/mes-device")
@Api(tags = "MesDeviceController", description = "设备管理")
public class MesDeviceController {

    @Autowired
    private MesDeviceServiceImpl mesDeviceService;

    @GetMapping("/GetDevices")
    @ApiOperation("获取设备列表")
    public CommonResult<IPage<MesDeviceVo>> GetDevices(@ApiParam("设备名称") @RequestParam( value = "name",required = false) String name,
                                                       @ApiParam("当前页") @RequestParam( value = "page",required = false) Integer page,
                                                       @ApiParam("每页行数") @RequestParam( value = "size",required = false) Integer size)
    {
          QueryWrapper<MesDeviceVo> queryWrapper = new QueryWrapper<>();
          if(!StringUtils.isBlank(name)) {
              queryWrapper.eq("Device_Type_No", name);
          }
           page = page == null || page < 1 ? 1 : page;
           size = size == null || size < 0 ? 2 : size;
          Page<MesDeviceVo> mesDeviceVoPage = new Page<>(page,size);
          IPage<MesDeviceVo> data =  mesDeviceService.getDevices(mesDeviceVoPage,queryWrapper);
        return  CommonResult.success(data);
    }

    @GetMapping("/AllDevices")
    @ApiOperation("获取所有设备列表")
    public CommonResult<List<MesDevice>> AllDevices()
    {
        List<MesDevice> data = new ArrayList<>();
        data =  mesDeviceService.list();
        return  CommonResult.success(data);
    }

    @GetMapping("/GetDevice")
    @ApiOperation("根据编号获取设备")
    public CommonResult<MesDevice> GetDeviceByNo(@ApiParam("设备编号") @RequestParam("no") String no)
    {
        if(no.isEmpty())
            return null;
        QueryWrapper<MesDevice> queryWrapper = new QueryWrapper<MesDevice>();
        queryWrapper.eq("Device_Type_No",no);
        return CommonResult.success(mesDeviceService.getOne(queryWrapper));
    }
    @PostMapping("/SaveDevice")
    @ApiOperation("保存设备")
    public CommonResult<Boolean> SaveDevice(@ApiParam("设备") @RequestBody MesDevice mesDevice) {
        if (mesDevice == null)
            return null;
        try {
            boolean result = false;
            if(mesDevice.id == 0L) {
                QueryWrapper<MesDevice> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("device_No", mesDevice.getDeviceNo());
                queryWrapper.or().eq("device_Name", mesDevice.getDeviceName());
                MesDevice mesTechnologyFlow1 = mesDeviceService.getOne(queryWrapper);
                if (mesTechnologyFlow1 != null) {
                    String msg = StrUtil.format("编号{}或名称{}已经存在！", mesTechnologyFlow1.getDeviceNo(), mesTechnologyFlow1.getDeviceName());
                    throw new ApiException(msg);
                }
                result = mesDeviceService.save(mesDevice);
            }
            else
            {
                result = mesDeviceService.update(mesDevice,null);
            }
            return CommonResult.success(result);
        } catch (Exception ex) {
            return CommonResult.failed(ex.getMessage());
        }
    }
    @DeleteMapping("/DeleteDevice/{Id}")
    @ApiOperation("删除设备")
    public CommonResult<Boolean> DeleteDevice(@ApiParam("设备ID") @PathVariable("Id") int Id) {
        try {
            boolean result = mesDeviceService.removeById(Id);
            return CommonResult.success(result);
        } catch (Exception ex) {
            return CommonResult.failed(ex.getMessage());
        }
    }

    @PostMapping("/getProcedureDeviceCapacity")
    @ApiOperation("根据工序和设备iD获取设备产能值")
    public CommonResult<?> GetProcedureDeviceCapacities (@RequestBody ProcedureDeviceCapacityDto procedureDeviceCapacityDto)
    {
       // ProcedureDeviceCapacityDto procedureDeviceCapacityDto = new ProcedureDeviceCapacityDto();
        QueryWrapper<ProcedureDeviceCapacityDto> queryWrapper = new QueryWrapper<ProcedureDeviceCapacityDto>();
         if( null != procedureDeviceCapacityDto )
         {
             if( null !=procedureDeviceCapacityDto.getProcedureIds() &&  procedureDeviceCapacityDto.getProcedureIds().length > 0)
             {
                 queryWrapper.in("a.id",procedureDeviceCapacityDto.getProcedureIds());
             }
             if( null != procedureDeviceCapacityDto.getDeviceId() && procedureDeviceCapacityDto.getDeviceId()> 0)
             {
                 queryWrapper.eq("f.device_id",procedureDeviceCapacityDto.getDeviceId());
             }
         }
        List<ProcedureDeviceParameterVo> result = mesDeviceService.getProcedureDeviceCapility(queryWrapper);
        return CommonResult.success(result);
    }

}
